<?php
namespace app\beautiful\controller\admin\setting;
use app\beautiful\model\StaffModel;
use app\BaseController;
use app\common\logic\DataLogic;

class ShopStaff extends BaseController{
    protected $name='员工';

    public function index()
    {
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        $staff = new StaffModel();
        $order=['add_time'=>'desc'];
        $field='*';
        $append=['jurisdiction','add_time_format'];
        $return = DataLogic::getDataList($staff,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        $return['miniapp_status'] = $this->request->shop->status;
        return $this->success($return);
    }

    public function show()
    {
        $staff_id = $this->request->param('staff_id/d',0);
        if($staff_id==0){
            return $this->success([]);
        }
        if(null === $staff = StaffModel::find($staff_id)){
            return $this->error($this->name.'不存在');
        }
        if($staff->is_delete != 0){
            return $this->error($this->name.'不存在');
        }
        if($staff->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }
        if($staff->status != 1){
            return $this->error($this->name.'已离职');
        }
        $detail=[
            'mobile'=>$staff->mobile,
            'name'=>$staff->name,
            'password'=>'******',
            'is_cashier_login'=>$staff->is_cashier_login,
//            'adviser_jurisdiction'=>$staff->adviserjurisdiction,
            'write_off_jurisdiction'=>$staff->write_off_jurisdiction,
            'add_value_jurisdiction'=>$staff->add_value_jurisdiction
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $name=$this->request->param('name');
        $mobile=$this->request->param('mobile');
        $password=$this->request->param('password');
        $is_cashier_login=$this->request->param('is_cashier_login/d',0);
//        $adviser_jurisdiction=$this->request->param('adviser_jurisdiction/d',0);
        $write_off_jurisdiction=$this->request->param('write_off_jurisdiction/d',0);
        $add_value_jurisdiction=$this->request->param('add_value_jurisdiction/a');
        $staff_id=$this->request->param('staff_id/d',0);
        if(empty($name)){
            return $this->error($this->name.'姓名未填写');
        }

        if(empty($mobile)){
            return $this->error('手机号未填写');
        }
        if(!is_mobile($mobile)){
            return $this->error('请填写合法的手机号');
        }
        if(empty($password)) return $this->error('请输入登录密码');
        if(!in_array($is_cashier_login,[0,1])){
            return $this->error('参数错误');
        }
        if(!in_array($write_off_jurisdiction,[0,1])){
            return $this->error('参数错误');
        }
//        if(!in_array($adviser_jurisdiction,[0,1])){
//            return $this->error('参数错误');
//        }
        if(empty($add_value_jurisdiction)){
            return $this->error('请给一点加值权限吧');
        }
        if(null !== $staff = StaffModel::find($staff_id)){
            if($staff->shop_id != $this->request->shopId){
                return $this->error('员工不存在');
            }

            if($staff->is_delete!=0){
                return $this->error($this->name.'不存在');
            }
            if($staff->status !=1){
                return $this->error($this->name.'已离职');
            }
            if($mobile!=$staff->mobile){
                $where=[
                    ['status','=',1],
                    ['mobile','=',$mobile],
                    ['is_delete','=',0]
                ];
                if(null !== StaffModel::where($where)->find()){
                    return $this->error('该手机号已经有了');
                }
            }
            if($password != '******'){
                $staff->password=md5($password);
            }

        }else{
            $where=[
                ['status','=',1],
                ['mobile','=',$mobile],
                ['is_delete','=',0]
            ];
            if(null !== StaffModel::where($where)->find()){
                return $this->error('该手机号已经有了');
            }
            $staff=new StaffModel();
            $staff->shop_id=$this->request->shopId;
            $staff->password=md5($password);
        }
        $staff->mobile=$mobile;
        $staff->name=$name;
//        $staff->adviser_jurisdiction=$adviser_jurisdiction;
        $staff->is_cashier_login=$is_cashier_login;
        $staff->write_off_jurisdiction=$write_off_jurisdiction;
        $staff->add_value_jurisdiction=$add_value_jurisdiction;
        $staff->save();
        return $this->success('操作成功');
    }

    public function leave()
    {
        $staff_id = $this->request->param('staff_id/d',0);
        if(null === $staff = StaffModel::find($staff_id)){
            return $this->error($this->name.'不存在');
        }
        if($staff->is_delete != 0){
            return $this->error($this->name.'不存在');
        }
        if($staff->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }
        if($staff->status != 1){
            return $this->error($this->name.'已离职');
        }

        //判断其他的（名义下还有会员）

        $staff->status=0;
        $staff->save();
        return $this->success('操作成功');
    }

    public function choose()
    {
        $type=$this->request->param('type');
        $staff_id = $this->request->param('staff_id/d',0);
        $keyword=$this->request->param('keyword');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['status','=',1]
        ];
        if($type=='member_detail'){
            if(!empty($staff_id)) $where[]=['staff_id','<>',$staff_id];
            if(!empty($keyword)) $where[]=['name|mobile','like','%'.$keyword.'%'];
            $where[]=['adviser_jurisdiction','=',1];
        }
        $staff = new StaffModel();
        $order=['add_time'=>'desc'];
        $field='staff_id,name,mobile,add_time';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($staff,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    // 技师离职
    public function quit()
    {
        $staff_id=$this->request->param('staff_id/d',0);
        if($staff_id==0) return $this->error($this->name.'不存在');
        if(null === $staff = StaffModel::find($staff_id)) return $this->error($this->name.'不存在');
        if($staff->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($staff->is_delete != 0) return $this->error($this->name.'不存在');

        if($staff->status != 1) return $this->error($this->name.'已离职，请勿重复操作');

        //补充判断

        $staff->status = -1;
        $staff->save();
        return $this->success('离职成功');
    }

    // 删除技师
    public function del()
    {
        $staff_id=$this->request->param('staff_id/d',0);
        if($staff_id==0) return $this->error($this->name.'不存在');
        if(null === $staff = StaffModel::find($staff_id)) return $this->error($this->name.'不存在');
        if($staff->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($staff->is_delete != 0) return $this->error($this->name.'不存在');

        if($staff->status != -1) return $this->error($this->name.'离职状态才可删除');

        //补充判断

        $staff->is_delete = 1;
        $staff->save();
        return $this->success('删除成功');
    }

}
